package cc.leap.web;

import cc.leap.constant.UserConstants;
import cc.leap.helper.LoginHelper;
import cc.leap.model.AjaxResult;
import cc.leap.model.domain.SysMenu;
import cc.leap.service.SysMenuService;
import cc.leap.utils.StringUtils;
import cn.dev33.satoken.annotation.SaCheckPermission;

import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Delete;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 登录验证
 *
 * @author leap
 */
@Valid
@Controller
@Mapping("/system/menu")
public class SysMenuController {
    @Inject
    private SysMenuService menuService;

    /**
     * 获取菜单列表
     */
    @SaCheckPermission("system:menu:list")
    @Get
    @Mapping("/list")
    public AjaxResult list(SysMenu menu) {
        Long userId = LoginHelper.getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
        return AjaxResult.success(menus);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @SaCheckPermission("system:menu:query")
    @Get
    @Mapping(value = "/{menuId}")
    public AjaxResult getInfo(Long menuId) {
        return AjaxResult.success(menuService.selectMenuById(menuId));
    }

    /**
     * 新增菜单
     */
    @SaCheckPermission("system:menu:add")
    @Post
    @Mapping
    public AjaxResult add(@Validated SysMenu menu) {
        if (!menuService.checkMenuNameUnique(menu)) {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath())) {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        menu.setCreateBy(LoginHelper.getUsername());
        return AjaxResult.success(menuService.insertMenu(menu));
    }

    /**
     * 删除菜单
     */
    @SaCheckPermission("system:menu:remove")
    @Delete
    @Mapping("/{menuId}")
    public AjaxResult remove(Long menuId) {
        if (menuService.hasChildByMenuId(menuId) > 0) {
            return AjaxResult.warn("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return AjaxResult.warn("菜单已分配,不允许删除");
        }
        return AjaxResult.success(menuService.deleteMenuById(menuId));
    }

    /**
     * 修改菜单
     */
    @SaCheckPermission("system:menu:edit")
    @Put
    @Mapping
    public AjaxResult edit(@Validated SysMenu menu) {
        if (!menuService.checkMenuNameUnique(menu)) {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath())) {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        } else if (menu.getMenuId().equals(menu.getParentId())) {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menu.setUpdateBy(LoginHelper.getUsername());
        return AjaxResult.success(menuService.updateMenu(menu));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @Get
    @Mapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(Long roleId) {
        Long userId = LoginHelper.getUserId();
        List<SysMenu> menus = menuService.selectMenuList(new SysMenu(), userId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ajax;
    }

    /**
     * 获取菜单下拉树列表
     */
    @Get
    @Mapping("/treeselect")
    public AjaxResult treeselect(SysMenu menu) {
        Long userId = LoginHelper.getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
        return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
    }

}
